extends Area2D

export var speed = 100
export var tank_scope = 200
export var scan_scope = 0.0
export var distacne:int = 200
export var move_dir:Vector2 = Vector2(0,1)
var init_pos:Vector2
var target_pos:Vector2
var forward:bool = true


func _ready():
	init_pos = position
	rotation = move_dir.angle()
	var tx = position.x + distacne * cos(rotation)
	var ty = position.y + distacne * sin(rotation)
	target_pos = Vector2(tx,ty)
#	position = Vector2(200,200)
	pass
	
func move_loop(delta):
	if position==init_pos :
		forward = true
	elif position == target_pos:
		forward = false
	if forward:
		rotation = position.direction_to(target_pos).angle()
		position = position.move_toward(target_pos,speed*delta)
	else:
		rotation = position.direction_to(init_pos).angle()
		position = position.move_toward(init_pos,speed*delta)

func _process(delta):
	var tank2 = get_node("../Tank2")
	#如果tank2不为空并且，在范围就进行控制
#	if position.distance_to(tank2.position) < tank_scope:
#		#方向的单位向量
#		var point_v = position.direction_to(tank2.position)
#		var r_v = Vector2(1*cos(rotation),1*sin(rotation))
#		var dot_result = point_v.dot(r_v)
#		print(rad2deg(acos(dot_result)))
#		if dot_result > scan_scope:	
#			rotation = point_v.angle()
#			position += speed * point_v * delta
#		else:
#			move_loop(delta)
#	else:
#		move_loop(delta)
#		var mp = position.direction_to(target_pos)
#		position += speed * delta * mp
#		print(position == target_pos)
#		print(position)
		
#		print(position == target_pos)
	
	var point_v = position.direction_to(tank2.position)
	var r_v = Vector2(1*cos(rotation),1*sin(rotation))
	var dot_result = point_v.dot(r_v)
	if position.distance_to(tank2.position) < tank_scope and dot_result > scan_scope:
		#方向的单位向量
		rotation = point_v.angle()
		position += speed * point_v * delta
	else:
		move_loop(delta)
